package utils;

import java.security.MessageDigest;
import java.util.Random;

public class MD5 {
    public static String getMD5(String plainText,int length){
        try{
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte b[] = md.digest();
            int i;

            StringBuffer buffer = new StringBuffer("");
            for (int offset = 0; offset <b.length;offset++){
                i=b[offset];
                if (i<0)
                    i+=256;
                if (i<16)
                    buffer.append("0");
                buffer.append(Integer.toHexString(i));
            }
            return buffer.toString().substring(0,length);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public static int getRandomCode(){
        int max = 9999;
        int min = 1111;
        Random random = new Random();
        return random.nextInt(max)%(max-min+1)+min;
    }
}
